#!/bin/bash
#mail:sopcce@qq.com
#data:2022年1月27日15:29:21
#自动初始化安装 Fastdfs
clear
echo -e '>>>>>> README <<<<<<
\033[32m## 自动初始化安装 Fastdfs  ##\033[0m'


##  软件下载路径
download_file_path="/usr/local"
download_file_url="https://gitee.com/sop_install/fastdfs-install/raw/master/zip/"
download_file_libfastcommon='libfastcommon-1.0.7.tar.gz'
download_file_nginx='nginx-1.16.1.tar.gz'
download_file_FastDFS='FastDFS_v5.05.tar.gz'
download_file_nginxFS='fastdfs-nginx-module_v1.16.tar.gz'

download_file_libfastcommon_fileName='libfastcommon-1.0.7'
download_file_nginx_fileName='nginx'
download_file_FastDFS_fileName='FastDFS'
download_file_nginxFS_fileName='fastdfs-nginx-module'
fastdfs_path="/etc/fdfs/"
fastdfs_web_port=8800


# 获取系统信息
sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1`
IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1`
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`




wget_download_file() {


	echo -e "=================================================================="
	echo -e "\033[32m 初始化fastdfs下载文件  \033[0m"
	echo -e "=================================================================="

	if [ ! -d ${download_file_path} ];then
		mkdir -p ${download_file_path} && cd ${download_file_path}
	else
		cd ${download_file_path}
	fi

	wget -P ${download_file_path} -O ${download_file_libfastcommon} ${download_file_url}${download_file_libfastcommon}
	wget -P ${download_file_path} -O ${download_file_nginx} ${download_file_url}${download_file_nginx}
	wget -P ${download_file_path} -O ${download_file_FastDFS} ${download_file_url}${download_file_FastDFS}
	wget -P ${download_file_path} -O ${download_file_nginxFS} ${download_file_url}${download_file_nginxFS}

	cd ${download_file_path}
    ls

	echo -e "=================================================================="
	echo -e "\033[32m 资源文件全部下载完毕  ${download_file_path} \033[0m"
	echo -e "=================================================================="

}

install_libfastcommon(){


	echo -e "=================================================================="
	echo -e "\033[32m 安装 libfastcommon 开始  \033[0m"
	echo -e "=================================================================="

	# 安转 libfastcommon
	cd ${download_file_path}
	tar -zxvf ${download_file_libfastcommon} -C ${download_file_path}
	cd ${download_file_libfastcommon_fileName}
	./make.sh
	./make.sh install
	cd /usr/lib64
	cp libfastcommon.so /usr/lib

	echo -e "=================================================================="
	echo -e "\033[32m 安装 libfastcommon 完成  \033[0m"
	echo -e "=================================================================="

}
install_fastdfs(){

	echo -e "=================================================================="
	echo -e "\033[32m 安装 fastdfs 开始  \033[0m"
	echo -e "=================================================================="

	cd ${download_file_path}
	tar -zxvf ${download_file_FastDFS} -C ${download_file_path}
	cd ${download_file_FastDFS_fileName}
	./make.sh
	./make.sh install
	cd conf
	cp * ${fastdfs_path}

	echo -e "=================================================================="
	echo -e "\033[32m 安装 fastdfs 完成  \033[0m"
	echo -e "=================================================================="

}
install_fastdfs_nginx_amodule(){
	echo -e "=================================================================="
	echo -e "\033[32m 安装 fastdfs_nginx_amodule 开始  \033[0m"
	echo -e "=================================================================="

	cd ${download_file_path}
	tar -zxvf ${download_file_nginxFS} -C ${download_file_path}
	cd ${download_file_nginxFS_fileName}/src/
	cp mod_fastdfs.conf ${fastdfs_path}

	## 配置 fastdfs_nginx_amodule
	time3=$(date "+%Y_%m_%d_%H_%M_%S")
	cp  config  config_bak_${time3}

	sleep 2

	old_str='/usr/local/'
	new_str='/usr/'
	sed -i 's#'''/usr/local/'''#'''/usr/'''#g' config
	sed -i 's#'''$old_str'''#'''$new_str'''#g' config


	cd ${fastdfs_path}
	sleep 2
	#cat mod_fastdfs.conf

	old_str='url_have_group_name = false'
	new_str='url_have_group_name = true'
	sed -i 's#'''$old_str'''#'''$new_str'''#g' mod_fastdfs.conf

	old_str='base_path=/tmp'
	new_str='base_path=/home/fastdfs'
	sed -i 's#'''$old_str'''#'''$new_str'''#g' mod_fastdfs.conf

	old_str='store_path0=/home/yuqing/fastdfs'
	new_str='store_path0=/home/fastdfs_storage'
	sed -i 's#'''$old_str'''#'''$new_str'''#g' mod_fastdfs.conf





	echo -e "=================================================================="
	echo -e "\033[32m 安装 fastdfs_nginx_amodule 完成  \033[0m"
	echo -e "=================================================================="
}

set_fastdfs_config(){

  echo -e "=================================================================="
  echo -e "\033[32m配置 fastdfs 开始  \033[0m"
  echo -e "=================================================================="

  cd ${fastdfs_path}
  cp tracker.conf.sample tracker.conf
  cp storage.conf.sample storage.conf
  cp client.conf.sample client.conf

  sleep 2
  #cat tracker.conf

  old_str='base_path=/home/yuqing/fastdfs'
  new_str='base_path=/home/fastdfs'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' tracker.conf

  old_str='http.server_port=8080'
  new_str='http.server_port=8000'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' tracker.conf

  sleep 2
  #cat storage.conf

  old_str='base_path=/home/yuqing/fastdfs'
  new_str='base_path=/home/fastdfs'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' storage.conf

  old_str='store_path0=/home/yuqing/fastdfs'
  new_str='store_path0=/home/fastdfs_storage'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' storage.conf


  old_str='tracker_server=192.168.209.121:22122'
  new_str='tracker_server='${IP}':22122'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' storage.conf

  old_str='http.server_port=8888'
  new_str='http.server_port=8800'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' storage.conf

  sleep 2
  #cat client.conf

  old_str='base_path=/home/yuqing/fastdfs'
  new_str='base_path=/home/fastdfs'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' client.conf


  old_str='tracker_server=192.168.0.197:22122'
  new_str='tracker_server='${IP}':22122'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' client.conf


  old_str='http.server_port=8080'
  new_str='http.server_port=8800'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' client.conf


  old_str='http.tracker_server_port=80'
  new_str='http.tracker_server_port=8800'
  sed -i 's#'''$old_str'''#'''$new_str'''#g' client.conf



  sleep 3

  /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
  /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

  sleep 3
  /usr/bin/fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/anti-steal.jpg


  echo -e "=================================================================="
  echo -e "\033[32m 配置 fastdfs 完成  \033[0m"
  echo -e "=================================================================="
}
system_init(){
  echo -e "=================================================================="
  echo -e "\033[32m系统初始化 开始  \033[0m"
  echo -e "=================================================================="
  # mkdir file
  yum install wget git unzip zip gcc gcc-c++ make cmake automake autoconf libtool perl pcre pcre-devel zlib zlib-devel openssl openssl-devel libevent vim -y

  mkdir -p /home/fastdfs/
  mkdir -p /home/fastdfs_storage
  mkdir -p /home/fastdfs_client
  mkdir -p /var/temp/nginx/client
  mkdir -p /usr/local/nginx/logs
  mkdir -p /usr/local/src
  mkdir -p /var/run/nginx
  mkdir -p ${fastdfs_path}

  echo -e "=================================================================="
  echo -e "\033[32m 系统初始化完成  \033[0m"
  echo -e "=================================================================="

}
system_check(){
	FDFS_T_CHECK=$(ps -ef|grep fdfs|grep -v grep|grep -v /usr/bin/fdfs_trackerd)
	FDFS_S_CHECK=$(ps -ef|grep fdfs|grep -v grep|grep -v /usr/bin/fdfs_storaged)
	NGINX_CHECK=$(ps -ef|grep nginx|grep master|grep -v /www/server/nginx)
	HTTPD_CHECK=$(ps -ef |grep -E 'httpd|apache'|grep -v /www/server/apache|grep -v grep)
	if [ "${FDFS_S_CHECK}" ] || [ "${FDFS_T_CHECK}" ] || [ "${NGINX_CHECK}" ] || [ "${HTTPD_CHECK}" ];then
		install_check
	else
      echo -e "\033[32m软件初始化完成，不存在安装软件 \033[0m"
	fi
}
install_check(){
	if [ "${INSTALL_FORCE}" ];then
		return
	fi
	echo -e "----------------------------------------------------"
	echo -e "检查已有其他nginx 环境，安装宝塔可能影响现有站点及数据"
	echo -e "Web/mysql service is alreday installed,Can't install panel"
	echo -e "----------------------------------------------------"
	echo -e "已知风险/Enter yes to force installation"
	read -p "输入yes强制安装: " yes;
	if [ "$yes" != "yes" ];then
		echo -e "------------"
		echo "取消安装"
		exit;
	fi
	INSTALL_FORCE="true"
}
## 获取当前系统的IP 地址
get_ip_address(){
	IP=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
	echo -e "\033[32mIP地址：${IP} \033[0m"
}

install_nginx(){
	echo -e "\033[32m install_nginx  \033[0m"
	cd /usr/local/
tar -zxvf nginx-1.16.1.tar.gz -C /usr/local
cd nginx-1.16.1

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/usr/local/fastdfs-nginx-module/src
make && make install

}

# 主函数统一调用
Install_Main() {
  startTime=`date +%s`

  get_ip_address
	sleep 5

	system_init
	sleep 5

	system_check
	sleep 5

  wget_download_file
	sleep 5

    # 解压文件
  install_libfastcommon
	sleep 5

  install_fastdfs
  sleep 5

  set_fastdfs_config
	sleep 5


	install_fastdfs_nginx_amodule
	sleep 5



	install_nginx



}
# 执行主函数
Install_Main
